import Request from './request.js'
import store from '../../store'
import {
	throttle
} from '../utils/throttle.js'
// need to change baseUrl

const baseUrl = process.env.NODE_ENV === 'development' ? "http://47.92.207.66:8082" : "http://47.92.207.66:8082"

const config = {
	baseUrl: baseUrl
}

const reqInterceptor = async (options) => {
	//TODO do your request interceptor, such as url/header config, token refresh...
	try {
		const value = uni.getStorageSync("TOKEN");
		// if (!value) {
		// 	delete options.header.Authorization
		// 	_requestLog(options, "请重新登录")
		// 	return false
		// }
		const _token = {
			'token': value
		};
		options.header = Object.assign({}, options.header, {
			'x-vs-language': store.getters['settings/getLang'] || 'zh_CN'
		}, _token);
		_requestLog(options, "成功通过")
		// return false will abort the request, and then reject a blank object {}
		return options
	} catch (e) {
		// error
	}
}

const resInterceptor = (response, conf = {}) => {
	// TODO do your response
	const statusCode = response.statusCode
	const errCode = response.data.errCode
	
	console.log(JSON.stringify(response.data) + "============")
	if (response.data.code == 10 || response.data.code==12 ) {
		uni.redirectTo({
			url:"/pages/my/login/login"
		})
	}

	if (errCode === '401') {
		response.statusCode = 401
		store.commit('user/setIs404', true)
		_responseLog(response, conf, "response 401")
		return {
			// 根据当前字段来判断是否reject
			wakaryReqToReject: true,
			msg: "请登录",
			res: response
		}
	}
	// response interceptor
	if (statusCode >= 200 && statusCode < 300) { //成功
		_responseLog(response, conf, "response 200-299")
		return response.data
	} else if (statusCode === 500) {
		_responseLog(response, conf, "response 500")
		// 为了对reject的内容更加可控，我们增加了一个控制字段 wakaryReqToReject
		return {
			// 根据当前字段来判断是否reject
			wakaryReqToReject: true,
			// 下面可以配置您的其它返回信息，方便您更加统一的处理reject的内容。
			// 以下内容会被作为reject的返回，根据您的需要处理，比如返回您的具体错误信息
			msg: "服务器错误",
			res: response
		}
	} else {
		_responseLog(response, conf, "response 300-499")
		// 为了对reject的内容更加可控，我们增加了一个控制字段 wakaryReqToReject

		return {
			// 根据当前字段来判断是否reject
			wakaryReqToReject: true,
			// 下面可以配置您的其它返回信息，方便您更加统一的处理reject的内容。
			// 以下内容会被作为reject的返回，根据您的需要处理，比如返回您的具体错误信息
			msg: "这里是提示信息",
			res: response
		}
	}
}

const req = new Request(config, reqInterceptor, resInterceptor)

// request log
function _requestLog(req, describe = null) {
	if (process.env.NODE_ENV === 'development') {
		console.log("地址：" + req.url)
		if (describe) {
			console.log("描述：" + describe)
		}
		console.log("详细：" + JSON.stringify(req))
	}
	//TODO into log server
}

// response log
function _responseLog(res, conf = {}, describe = null) {
	let _statusCode = res.statusCode;
	if (process.env.NODE_ENV === 'development') {
		console.log("地址: " + conf.url)
		if (describe) {
			console.log("描述：" + describe)
		}
		console.log("结果: " + JSON.stringify(res))
	}
	//TODO into log server
	if (_statusCode === 500) {
		// 
	} else if (_statusCode === 401) {
		console.log(throttle)
		throttle.canDoFunction({
			key: "is401",
			time: 1000,
			success: () => {
				uni.showToast({
					title: '请登录',
					icon: "none"
				})
				uni.navigateTo({
					url: "/pages/my/login/login"
				})
			}
		})
	}
}

export default req
